package com.xyebank.ledger.manage;

import com.xyebank.ledger.enums.TaskStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class ManageRedisTool {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 使用锁的方式来设置状态
     * @param jobType
     * @return
     */
    @SuppressWarnings("unchecked")
    public boolean setWorkStatus(String jobType) {
        
        
		ValueOperations<String, String> values = redisTemplate.opsForValue();
        return values.setIfAbsent(buildKey(jobType, "self_status"), TaskStatus.WORK.name());
    }

    public void updateWorkStatus(String jobType) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "self_status"), TaskStatus.WORK.name(), 30, TimeUnit.SECONDS);
    }

    /**
     * 清空锁
     * @param jobType
     */
    public void removeWorkStatus(String jobType) {
        redisTemplate.delete(buildKey(jobType, "self_status"));
    }

    /**
     * 判断指定的job的T1是否在工作中
     *
     * @param jobType
     * @return
     */
    public boolean isT1Working(String jobType) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        String o = values.get(buildKey(jobType, "T1_status"));
        if (o != null) {
            return TaskStatus.WORK.name().equals(o);
        }
        return false;
    }

    /**
     *
     * @param jobType
     */
    public void setT1Working(String jobType) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "T1_status"), TaskStatus.WORK.name(),30, TimeUnit.SECONDS);
    }



    /**
     * 设置指定的job的T1状态为工作中
     *
     * @param jobType
     */
    public void setT1StatusFinWork(String jobType) {
        ValueOperations<String, String> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "T1_status"), TaskStatus.NONE.name());
    }


    /**
     * 获取指定job的DB下标
     * @param jobType
     * @return
     */
    public Integer getDBIndex(String jobType) {
        ValueOperations<String, Integer> values = redisTemplate.opsForValue();
        Integer index = values.get(buildKey(jobType, "mig_index"));
        if(index == null) {
            return 0;
        }
        return index;
    }

    /**
     * 更新指定job的DB下标
     * @param jobType
     * @param curIdx
     * @param add
     */
    public void updateDBIndex(String jobType, Integer curIdx, Integer add) {
        ValueOperations<String, Integer> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "mig_index"), curIdx + add);
    }

    /**
     * 让指定的job的查询次数自增
     *
     * @param jobType
     */
    public void increseQueryCount(String jobType) {
        ValueOperations<String, Integer> values = redisTemplate.opsForValue();
        //应该先判断存在不，然后再处理
        if(values.get(buildKey(jobType, "count")) != null) {
//            values.increment(buildKey(jobType, "count"), 1);
//            values.increment(buildKey(jobType, "count"), 1);
            Integer old = values.get(buildKey(jobType, "count"));
            values.set(buildKey(jobType, "count"), old + 1);
        } else {
            values.set(buildKey(jobType, "count"), 1);
        }
    }

    /**
     * 清空指定的job的查询次数
     *
     * @param jobType
     */
    public void resetQueryCount(String jobType) {
        ValueOperations<String, Integer> values = redisTemplate.opsForValue();
        values.set(buildKey(jobType, "count"), 0);
    }

    /**
     * 更新指定的job的最后查询时间
     *
     * @param jobType
     */
    public void updateLastDate(String jobType) {
        redisTemplate.opsForValue().set(buildKey(jobType, "lastDate"), new Date());
    }

    public void rm(String key) {
        redisTemplate.delete(key);
    }
    public void rmx(String key) {
        redisTemplate.delete(key+"mig_index");
    }

    /**
     * 构造key
     *
     * @param title
     * @param key
     * @return
     */
    private String buildKey(String title, String key) {
        return title + key;
    }

    public TaskStatus getWorkStatus(String jobType) {
        ValueOperations<String, TaskStatus> values = redisTemplate.opsForValue();
        return values.get(buildKey(jobType, "self_status"));
    }

}
